import smtplib
from email.mime.text import MIMEText
from typing import Optional
import os

class EmailSender:
    def __init__(self):
        self.smtp_host = os.getenv("SMTP_HOST", "smtp.189.cn")
        self.smtp_port = int(os.getenv("SMTP_PORT", 587))
        self.smtp_user = os.getenv("SMTP_USER", "xujunslf@189.cn")
        self.smtp_password = os.getenv("SMTP_PASSWORD", "Vq)8Rs(3o*8Wr(3M")

    def send_email(
        self,
        to_email: str,
        subject: str,
        body: str,
        from_email: Optional[str] = None
    ) -> bool:
        msg = MIMEText(body)
        msg["Subject"] = subject
        msg["From"] = from_email or self.smtp_user
        msg["To"] = to_email

        try:
            with smtplib.SMTP(self.smtp_host, self.smtp_port) as server:
                server.starttls()
                server.login(self.smtp_user, self.smtp_password)
                server.send_message(msg)
            return True
        except Exception as e:
            print(f"Email sending failed: {str(e)}")
            return False
